﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grishko.DataTier.SqlLibrary
{
    public class SqlWord
    {
        public enum SqlWordType {Like,DoNotLike,NotParamLike,Function};

        /// <summary>
        /// параметр функции либо опреатора like
        /// </summary>
        public class WordParametr
        {
            string _value = string.Empty;
            public WordParametr(string Value)
            {
                _value = Value;
            }
            public bool isBetweenParametr 
            {
                get 
                {
                    return  (_value.IndexOf("[") == 0) ;
                }
            }
            public string SQLValue
            {
                get
                {
                

                    if (isBetweenParametr) 
                            return GetBetweenSql();               
                    return "'%" + _value + "%'";
                }
            }
            

            //return "("+ParamTxt+"='"+s1+"'"+" and dbo.CompareValueText_Between(ValueTxt,'"+s2+"','"+ s3 + "')=1)";
            private string GetBetweenSql()
            {

                string s = _value.Replace("[", "");
                s = s.Replace("]", "");
                if (s.IndexOf("-") == -1)
                    return "dbo.CompareValueText_Between(ValueTxt,'" + s + "','" + s + "')=1";
                else
                    return "dbo.CompareValueText_Between(ValueTxt,'" + s.Substring(0, s.IndexOf("-")) + "','" + s.Substring (s.IndexOf("-")+1) + "')=1";

            }

        }

        protected const string ParamTxt = "ParamTxt";
        protected const string ValueTxt = "ValueTxt";
        protected  char[] ParamSplit = ",".ToCharArray() ;


        public virtual SqlWordType WordType 
            {
                get { return SqlWordType.Like; } 
            }

        public virtual string ParamString { get; set; }
      
        public virtual string GetSql() 
            {                
                Prepare(); 
                return string.Empty; 
            }

       

        public virtual string ID { get; set; }    

        /// <summary>
        /// считывает оператор и формирует набор WordParametr
        /// </summary>
        protected virtual void Prepare()
        {
            
        }

        private List<WordParametr> _Paramentrs = new List<WordParametr>();
        protected List<WordParametr> Paramentrs { get { return _Paramentrs; } }
        protected virtual string ParamTxtValue { get { return string.Empty; } }


    }

    
    
}
